/**
 * 
 */
package edu.bu.grocery.backingbean;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.validation.constraints.NotNull;

import edu.bu.grocery.session.UserSession;
import edu.bu.grocerylist.model.GroceryUser;
import edu.bu.grocerylist.service.IGroceryUserService;


/**
 * JSF backingbean to link the login page with the back end code
 * @author donlanp
 *
 */
public class LoginBackingBean  implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6911999452515504528L;

	private UserSession session;
	
	private IGroceryUserService userService;
	
	@NotNull
	private String username;
	@NotNull
	private String password;
	
	/**
	 * Performs the login functionality.  If the login fails, we notify the user.
	 * @return If successful, the JSF string to navigate to the new page (grocerylists)
	 */
	public String login(){
		GroceryUser user = userService.login(username, password);
		
		if(user == null){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Incorrect username/password!"));
			return "";
		} else {
			session.setCurrentUser(user);
		}
		
		return "login";
	}
	
	//Getters and Setters

	/**
	 * @return the session
	 */
	public UserSession getSession() {
		return session;
	}

	/**
	 * @param session the session to set
	 */
	public void setSession(UserSession session) {
		this.session = session;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the service
	 */
	public IGroceryUserService getUserService() {
		return userService;
	}

	/**
	 * @param service the service to set
	 */
	public void setUserService(IGroceryUserService service) {
		this.userService = service;
	}
	
	
	
}
